package net.keebook.webapp.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;

public class Mail {
	private Session session ;
	private final String host;
	private final String username;
	private final String password;
	private final boolean auth;
	private final boolean enable;
	
	public Mail(String host, int port, boolean auth, String username, String password, boolean enable) {
		this.host = host;
		this.auth = auth;
		this.username = username;
		this.password = password;
		this.enable = enable;
		Properties props = System.getProperties();
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.port", port);
		session = Session.getDefaultInstance(props, null);
	}
	
	private Session getSession() {
		return session;
	}
	
	public MimeMessage createMimeMessage(String senderEmail, String senderName) throws MessagingException, UnsupportedEncodingException {
		MimeMessage message = new MimeMessage(getSession());
		message.setFrom(new InternetAddress(senderEmail, senderName));
		return message;
	}
	
	public void sendMail(String senderEmail, String senderName, String recipient, String recipientName, String subject, String text) throws AddressException, MessagingException, UnsupportedEncodingException {
		MimeMessage message = createMimeMessage(senderEmail, senderName);
		if (recipientName == null) {
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
		} else {
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient, recipientName));
		}
		message.setSubject(subject, "UTF-8");
		message.setText(text, "UTF-8");
		message.setHeader("Content-Type", "text/plain; charset=UTF-8");
		sendMail(message);
	}
	
	public void sendMail(MimeMessage message) throws AddressException, MessagingException {
		try {
			Logger.getLogger(this.getClass()).info("Sending mail to " + message.getRecipients(Message.RecipientType.TO).toString() + " with content: " + new String(message.getContent().toString()).replace("\n", "\\n"));
		} catch (IOException e) {
			Logger.getLogger(this.getClass()).info("Sending mail to " + message.getRecipients(Message.RecipientType.TO).toString() + " with unknown content");
		}
		
		if (!isEnable()) {
			return;
		}
		
		Transport tr = getSession().getTransport("smtp");
		if (!isAuth()) {
			tr.connect();
		} else {
			tr.connect(
				getHost(),
				getUsername(),
				getPassword()
			);
		}
		message.saveChanges();
		tr.sendMessage(message, message.getAllRecipients());
		tr.close();
	}

	public String getHost() {
		return host;
	}

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

	public boolean isAuth() {
		return auth;
	}

	public boolean isEnable() {
		return enable;
	}
}
